package com.example.demo8;

import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.Image;
import javafx.scene.input.KeyCode;
import javafx.scene.paint.Color;
import java.util.ArrayList;
import java.util.List;

public class Map {
    private static final int TILE_SIZE = 40;
    private static final int MAP_WIDTH = 38;
    private static final int MAP_HEIGHT = 23;
    private int playerX = 2;
    private int playerY = 2;
    private Canvas canvas;
    private GraphicsContext gc;
    private List<Image> mapImages; // 存储多个地图图片
    private List<int[][]> maps; // 存储多个地图的障碍物布局
    private int currentMapIndex; // 当前地图的索引

    public Map(Canvas canvas) {
        this.canvas = canvas;
        this.gc = canvas.getGraphicsContext2D();
        initMaps();
        currentMapIndex = 0;
    }

    private void initMaps() {
        mapImages = new ArrayList<>();
        maps = new ArrayList<>();

        // 初始化地图图片
        mapImages.add(new Image(getClass().getResourceAsStream("/map1.png")));
        mapImages.add(new Image(getClass().getResourceAsStream("/map2.png")));

        // 初始化地图障碍物布局
        maps.add(generateMap1());
        maps.add(generateMap2());
    }

    private int[][] generateMap1() {
        int[][] map = new int[MAP_WIDTH][MAP_HEIGHT];
        for (int x = 0; x < map.length; x++) {
            for (int y = 0; y < map[0].length; y++) {
                map[x][y] = (x == 0 || x == map.length - 1 || y == 0 || y == map[0].length - 1) ? 1 : 0;
            }
        }
        java.util.Random random = new java.util.Random();
        for (int i = 0; i < 20; i++) {
            int x = random.nextInt(map.length - 4) + 2;
            int y = random.nextInt(map[0].length - 4) + 2;
            map[x][y] = 1;
        }
        return map;
    }

    private int[][] generateMap2() {
        int[][] map = new int[MAP_WIDTH][MAP_HEIGHT];
        for (int x = 0; x < map.length; x++) {
            for (int y = 0; y < map[0].length; y++) {
                map[x][y] = (x == 0 || x == map.length - 1 || y == 0 || y == map[0].length - 1) ? 1 : 0;
            }
        }
        java.util.Random random = new java.util.Random();
        for (int i = 0; i < 30; i++) {
            int x = random.nextInt(map.length - 4) + 2;
            int y = random.nextInt(map[0].length - 4) + 2;
            map[x][y] = 1;
        }
        return map;
    }

    public int[][] getCurrentMap() {
        return maps.get(currentMapIndex);
    }

    public void nextMap() {
        currentMapIndex = (currentMapIndex + 1) % mapImages.size();
    }

    public void drawMap(GraphicsContext gc) {
        Image currentMapImage = mapImages.get(currentMapIndex);
        gc.drawImage(currentMapImage, 0, 0, MAP_WIDTH * TILE_SIZE, MAP_HEIGHT * TILE_SIZE);

        for (int y = 0; y < MAP_HEIGHT; y++) {
            for (int x = 0; x < MAP_WIDTH; x++) {
                gc.setStroke(Color.rgb(100, 100, 100, 0.5));
                gc.strokeRect(x * TILE_SIZE, y * TILE_SIZE, TILE_SIZE, TILE_SIZE);
            }
        }
    }

    public void handleKeyPressed(KeyCode code) {
        int newX = playerX;
        int newY = playerY;

        if (code == KeyCode.UP && playerY > 0) newY--;
        if (code == KeyCode.DOWN && playerY < MAP_HEIGHT - 1) newY++;
        if (code == KeyCode.LEFT && playerX > 0) newX--;
        if (code == KeyCode.RIGHT && playerX < MAP_WIDTH - 1) newX++;

        // 更新玩家位置
        playerX = newX;
        playerY = newY;
    }
}